#include "ComplexSprite.h"

ComplexSprite::ComplexSprite()
{
   sprite = new Sprite();
}

ComplexSprite::~ComplexSprite()
{

}
	
void ComplexSprite::SetImage(Image* i)
{
   sprite->SetImage(i);
}
	
void ComplexSprite::AddCollisionBox(double x, double y, double w, double h)
{
   Rectangle t_rec;
   t_rec.x = x;
   t_rec.y = y;
   t_rec.width = w;
   t_rec.height = h;
   collisionVec.push_back(t_rec);
}

void ComplexSprite::ClearCollisionBoxes()
{
   collisionVec.clear();
}
	
void ComplexSprite::Draw()
{
   sprite->Draw();
}
	
void ComplexSprite::SetRotation(u32 ang)
{
   sprite->SetRotation(ang);
}

void ComplexSprite::SetZoom(u32 zoom_val)
{
   sprite->SetZoom(zoom_val);
}

void ComplexSprite::SetPosition(u32 x, u32 y)
{
   sprite->SetPosition(x,y);
}

Rectangle* ComplexSprite::BoxAt(unsigned int i)
{
   if(collisionVec.size() > i)
		return &(collisionVec.at(i));
		
	return NULL;
}

int ComplexSprite::GetCollisionBoxCount()
{
	return collisionVec.size();
}

Sprite* ComplexSprite::GetSprite()
{
	return sprite;
}

bool ComplexSprite::CollidesWith(ComplexSprite* s,bool check)
{
	if(collisionVec.size() <= 0)
	{
		if(s->GetCollisionBoxCount() > 0)
		{
			for(int i = 0; i < s->GetCollisionBoxCount(); i++)
			{
				if(sprite->CollidesWith(s->BoxAt(i),s->GetSprite()->GetX(), s->GetSprite()->GetY()),check)
				{
					return true;
				}
			}
		}
		else
		{
			return sprite->CollidesWith(s->GetSprite(),check);
		}
	}
	else
	{
		if(s->GetCollisionBoxCount() > 0)
		{
			for(unsigned int i = 0; i < collisionVec.size(); i++)
			{
				for(int j = 0; j < s->GetCollisionBoxCount(); j++)
				{
					Sprite tsprite;
					Rectangle* rec = s->BoxAt(j);
					tsprite.DefineCollisionRectangle(rec->x, rec->y, rec->width, rec->height);
					if(tsprite.CollidesWith(&(collisionVec.at(i)),sprite->GetX(), sprite->GetY()),check)
					{
						return true;
					}
				}
			}
		
		}
		else
		{
			for(unsigned int i = 0; i < collisionVec.size(); i++)
			{
				if(s->GetSprite()->CollidesWith(&(collisionVec.at(i)),sprite->GetX(), sprite->GetY()),check)
				{
					return true;
				}
			}
		}
	}
	
	return false;
}
	
bool ComplexSprite::CollidesWith(Sprite* s,bool check)
{
	//if(collisionVec.size() <= 0)
	//{
		return sprite->CollidesWith(s, check);
	//}
	//else
	//{
	//	for(unsigned int i = 0; i < collisionVec.size(); i++)
	//	{
	//		if(s->CollidesWith(&(collisionVec.at(i)),sprite->GetX(), sprite->GetY()),check)
	//		{
	//			return true;
	//		}
	//	}
	//}
	
	//return false;
}
